package com.pingan.stat.reposys.general.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;

import com.pingan.common.core.property.Properties;
import com.pingan.common.util.ShellExecutor;
import com.pingan.setting.general.entity.GeneralStatSettingBean;
import com.pingan.setting.general.service.GeneralStatSettingService;
import com.pingan.stat.service.StatService;
import com.pingan.stat.service.TaskSubmitterService;

@Component
public class GeneralOldCookieUserStatService implements StatService {

	@Properties(name = "reposys.general.output.path")
	private String outputPath;

	@Properties(name = "reposys.general.oldcookieuser")
	private String generalOldCookieUserShell;

	@Properties(name = "reposys.general.oldcookieuser.domain")
	private String generalOldCookieUserDomainShell;

	@Properties(name = "reposys.general.oldcookieuser.entrance")
	private String generalOldCookieUserEntranceShell;

	@Resource
	private GeneralStatSettingService generalStatSettingService;

	@Resource
	private TaskSubmitterService taskSubmitterService;

	@Resource
	private ShellExecutor shellExecutor;

	private static final Log log = LogFactory.getLog("hive");

	@Override
	public void dailyStat(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String yyyyMMdd = sdf.format(date);
		String output = outputPath + "/" + yyyyMMdd;

		List<GeneralStatSettingBean> beans = generalStatSettingService
				.getGeneralStatSettingBeans();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		String timeStr = Long.valueOf(cal.getTimeInMillis()).toString();

		try {
			log.info("----------------->>>>>>>>>>>>>>>>>> stat old cookie user start....");
			taskSubmitterService.submit(beans, generalOldCookieUserShell,
					yyyyMMdd, output, timeStr);
			log.info("----------------->>>>>>>>>>>>>>>>>> stat old cookie user end");
			log.info("----------------->>>>>>>>>>>>>>>>>> stat old cookie user top-domain start....");
			taskSubmitterService.submit(beans, generalOldCookieUserDomainShell,
					yyyyMMdd, output, timeStr);
			log.info("----------------->>>>>>>>>>>>>>>>>> stat old cookie user top-domain end");
			log.info("----------------->>>>>>>>>>>>>>>>>> stat old cookie user top-entrance start....");
			taskSubmitterService.submit(beans,
					generalOldCookieUserEntranceShell, yyyyMMdd, output,
					timeStr);
			log.info("----------------->>>>>>>>>>>>>>>>>> stat old cookie user top-entrance end");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
